<?php

class ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }

        $priority = null;
        $message = null;
        $error_no = null;
        $data = null;
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
//                $this->getResponse()->setHttpResponseCode(404);
//                $this->view->message = 'Page not found';
                $priority = Zend_Log::CRIT;
                $message = '您访问的页面没有找到！';
                $error_no = "404";
                $exceptionType = 'SYS_EXCEPTION';
                break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
//                $this->getResponse()->setHttpResponseCode(401);
//                $this->view->message = $errors->exception->getMessage();
                $priority = Zend_Log::CRIT;
                $message =  $errors->exception->getMessage();;
                $error_no = "401";
                $exceptionType = 'SYS_EXCEPTION';
                break;
            case Jwawa_Controller_Plugin_ErrorHandler::EXCEPTION_BUSINESS_ERROR:
//                $this->getResponse()->setHttpResponseCode(401);
//                $this->view->message = $errors->exception->getMessage();
                $priority = Zend_Log::INFO;
                $message =  $errors->exception->getMessage();
                $error_no = $errors->exception->error_no;
                $data = $errors->exception->data;
                $exceptionType = 'BIZ_EXCEPTION';
                break;

            default:
                // application error
//                $this->getResponse()->setHttpResponseCode(500);
//                $this->view->message = 'Application error';
                $priority = Zend_Log::CRIT;
                $message =  $errors->exception->getMessage();;
                $error_no = "500";
                $exceptionType = 'SYS_EXCEPTION';
                break;
        }

        if($message == null){
            $errorResources = Zend_Registry::get('errorResources');
            $message = resStringReplace($errorResources->get($error_no), $errors->exception->param);
        }

        // reponse to client
//        $isAjax = $this->_request->getParam('isajax');
//        if( $isAjax === true){
            $this->_helper->layout->disableLayout();
            $view_renderer = $this->_helper->getHelper("viewRenderer");
            $view_renderer->setNoRender(true);
            $rst = array();
            $rst["success"] = false;
            $rst["error_no"] = $error_no;
            $rst["message"] = $message;
            $rst["data"] = $data;
            $rst["exceptionType"] = $exceptionType;
            $json = Zend_Json::encode($rst);
            echo $json;
//        } else {
//            $this->_helper->layout->enableLayout();
//            $view_renderer = $this->_helper->getHelper("viewRenderer");
//            $view_renderer->setNoRender(false);
//        }
//

        // Log exception, if logger available
        $this->view->message = $message;
        if ($log = $this->getLog()) {
            $log->setEventItem('file', $errors->exception->getFile());
            $log->setEventItem('line', $errors->exception->getLine());
//            $log->log(mb_convert_encoding($this->view->message, "GBK", "auto"), $priority, $errors->exception);
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters:' . PHP_EOL . print_r($errors->request->getParams(), 1), $priority, $errors->request->getParams());
            $log->log('PHP Stack trace:' . PHP_EOL . $errors->exception->getTraceAsString(), $priority);
        }

        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }

        $this->view->request = $errors->request;
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}

